using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour
{

    private Vector3 movementSum = Vector3.zero;

    private const int MAXIMUM_SPEED = 10;

    private Vector3 groundSphere = new Vector3(0, -0.2f, 0);

    private int playerParts = 0;

    private const string SPRINT = "Sprint";

    private bool sideWaysOnly = false;

    public bool SideWaysOnly
    {
        get { return sideWaysOnly; }
        set { sideWaysOnly = value; }
    }


    // Use this for initialization
    void Start()
    {
        playerParts = Physics.OverlapSphere(groundSphere + this.transform.position, 0.4f).Length;
    }

    // Update is called once per frame
    void Update()
    {
        ProcessMovement();
    }

    void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawSphere(groundSphere + this.transform.position, 0.4f);
    }
    void ProcessMovement()
    {
        if (Physics.OverlapSphere(groundSphere + this.transform.position, 0.4f).Length > playerParts)
        {
            //#if UNITY_ANDROID

            //#if UNITY_EDITOR

            //movementSum.z = Input.acceleration.x * MAXIMUM_SPEED * 1.5f * (float)Mathf.Cos(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad) +
            //                Input.acceleration.y * MAXIMUM_SPEED * 1.5f * (float)Mathf.Sin(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad);
            //movementSum.x = Input.acceleration.x * MAXIMUM_SPEED * 1.5f * (float)Mathf.Sin(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad) +
            //                Input.acceleration.y * MAXIMUM_SPEED * 1.5f * -(float)Mathf.Cos(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad);

            //#else
        //    #if UNITY_ANDROID
            movementSum.x = Input.acceleration.x * MAXIMUM_SPEED * 2f * (float)Mathf.Cos(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad) +
                            Input.acceleration.y * MAXIMUM_SPEED * 2f * (float)Mathf.Sin(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad);
            movementSum.z = Input.acceleration.x * MAXIMUM_SPEED * 2f * -(float)Mathf.Sin(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad) +
                            Input.acceleration.y * MAXIMUM_SPEED * 2f * (float)Mathf.Cos(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad);
         //   #endif
            //#endif
            //#endif

            #if UNITY_STANDALONE
            float horizontal = Mathf.Pow(Input.GetAxis("Horizontal"),3);
            float vertical = Mathf.Pow(Input.GetAxis("Vertical"), 3) + 0.2f;


            movementSum.x = horizontal * MAXIMUM_SPEED * (float)Mathf.Cos(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad) +
                            vertical * MAXIMUM_SPEED * (float)Mathf.Sin(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad);
            movementSum.z = horizontal * MAXIMUM_SPEED * -(float)Mathf.Sin(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad) +
                            vertical * MAXIMUM_SPEED * (float)Mathf.Cos(Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad);
            #endif



            this.SidewaysCheck();

            this.NormalizeMovement();

            this.BrakeCheck();
        }
        else
        {
            movementSum = Vector3.zero;

        }
        this.constantForce.force = movementSum;
    }


    private void BrakeCheck()
    {
        if (Input.GetMouseButton(0) ||
              (movementSum.z == 0 && movementSum.x == 0))
        {
            rigidbody.angularVelocity = rigidbody.angularVelocity * 0;
            movementSum = -movementSum * 0.5f;
        }

    }


    private void SidewaysCheck()
    {
        if (this.sideWaysOnly)
        {
            if (Mathf.Abs(movementSum.x) >= Mathf.Abs(movementSum.z))
                movementSum.z = 0;
            else
                movementSum.x = 0;
        }
    }


    private void NormalizeMovement()
    {
        CutOff(ref this.movementSum.x);
        CutOff(ref this.movementSum.z);
    }

    private void CutOff(ref float vector)
    {
        if (vector > MAXIMUM_SPEED)
            vector = MAXIMUM_SPEED;
        else if (vector < -MAXIMUM_SPEED)
            vector = -MAXIMUM_SPEED;
    }
}
